package com.gaoch.simweather.ui.weather

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gaoch.simweather.MyApplication
import com.gaoch.simweather.R
import com.gaoch.simweather.logic.model.HourlyResponse


class HourAdapter(private val context:Context, private val hourList: List<HourlyResponse.HourWeather>): RecyclerView.Adapter<HourAdapter.ViewHolder>() {

    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val hourTime:TextView=view.findViewById(R.id.item_hour_time)
        val hourTemp:TextView=view.findViewById(R.id.item_hour_temp)
        val hourWind:TextView=view.findViewById(R.id.item_hour_wind)
        val hourIcon:ImageView=view.findViewById(R.id.item_hour_iv)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.item_hour,parent,false)
        val holder=ViewHolder(view)
        return holder
    }

    override fun getItemCount()=hourList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val hour=hourList[position]
        holder.hourTime.text=data2simData1(hour.fxTime)
        holder.hourTemp.text="${hour.temp}℃"
        holder.hourWind.text=hour.windDir
        holder.hourIcon.setImageResource(context.resources.getIdentifier("weather_icon_${hour.icon}","drawable",context.packageName))
    }

    private fun data2simData1(origin_date: String): String? {
        val origin = origin_date.split("T".toRegex()).toTypedArray()
        val odate = origin[1].split("\\+".toRegex()).toTypedArray()
        return odate[0]
    }
}